<?php
namespace APP\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;

class Student extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $table = 'crm_student';

    protected $keyType = 'int'; // 这里添加了分号

    public $incrementing = true;

    protected $fillable = [
        'student_no',
        'name',
        'sex',
        'age',
        'address',
        'mobile',
        'class_id',
        'status'
    ];

    protected $casts = [
        'sex' => 'integer',
        'age' => 'integer',
        'class_id' => 'integer',
        'status' => 'boolean',
        'created' => 'datetime:Y-m-d H:i:s',
        'updated' => 'datetime:Y-m-d H:i:s'
    ];
    protected $dates = ['deleted_at'];

    protected $hidden = [
    ];

    public function getSexTextAttribute()
    {
        $sexMap = [
            1 => '男',
            2 => '女',
            null => '未知'
        ];
        return $sexMap[$this->sex] ?? '未知';
    }

    public function setMobileAttribute($value)
    {
        $cleanMobile = preg_replace('/[\s\-()]/', '', $value);
        $this->attributes['mobile'] = $cleanMobile;
    }
}